/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 有点呆
 * Date: 2024-11-07
 * Time: 13:16
 */

import java.util.Scanner;
class HRRN{
    int number;
    Works[] works;
    int NowTime = 0;
    int NowData;
    //数据的传入
    public void CreatWorks(){
        Scanner scanner=new Scanner(System.in);
        number=scanner.nextInt();
        works=new Works[number];
        for (int i=0;i<works.length;i++){
            works[i]=new Works();
        }
        for (int i=0;i<works.length;i++){
            works[i].WorkName=scanner.next();
        }
        for (int i=0;i<works.length;i++){
            works[i].T_Come=scanner.nextInt();
        }
        for (int i=0;i<works.length;i++){
            works[i].T_Serve=scanner.nextInt();
        }
    }
    //数据排序
    public void SortWorks(){
        for (int i=0;i< works.length;i++){
            for (int j=i;j< works.length;j++){
                if (works[i].T_Come>works[j].T_Come){
                    int temp1=works[i].T_Come;//交换到达时间
                    works[i].T_Come=works[j].T_Come;
                    works[j].T_Come=temp1;

                    String temp2 = works[i].WorkName;//交换作业名
                    works[i].WorkName = works[j].WorkName;
                    works[j].WorkName = temp2;

                    float temp3 = works[i].T_Serve;//交换服务时间
                    works[i].T_Serve = works[j].T_Serve;
                    works[j].T_Serve = temp3;
                }
            }
        }
    }
    //找到第一个未完成的作业
    public int FindMinArrival(){
        int firstwork=0;
        for (int i=0;i< works.length;i++){
            if (works[i].flag==0){
                return i;
            }
        }
        return 0;
    }
    //找到当前响应比最高的作业
    public int FindResponse( ) {
        int max = 0;
        for (int i = 0; i < works.length; i++) {
            if (works[i].flag == 0 && works[i].T_Come<=NowTime) {
                int wait=NowTime-works[i].T_Come;
                works[i].Response = (wait / works[i].T_Serve);
                if (works[i].Response<0){
                    works[i].Response=-works[i].Response;
                }
            }
            max = FindMinArrival();
        }
        for (int i = 0; i < works.length; i++) {
            if (works[i].flag == 0 && works[i].T_Come<=NowTime && works[i].Response >= works[max].Response) {
                max = i;
            }
        }
        return max;
    }
    //计算数据
    public void DataCalculate(){
        //完成时间
        for (int i=0;i< works.length;){
            NowData=FindResponse( );
            if (works[NowData].T_Come<=NowTime){
                works[NowData].flag=1;
                works[NowData].T_Compelet=(int)(NowTime+works[NowData].T_Serve);
                NowTime=works[NowData].T_Compelet;
                i++;
            }
            else {
                NowData=FindMinArrival();
                works[NowData].flag=1;
                works[NowData].T_Compelet=(int)(works[NowData].T_Come+works[NowData].T_Serve);
                NowTime=works[NowData].T_Compelet;
                i++;
            }
        }
        //周转时间
        for (int i=0;i< works.length;i++){
            works[i].T_Need=works[i].T_Compelet-works[i].T_Come;
        }
        //带权周转时间
        for (int i=0;i< works.length;i++){
            works[i].T_Weight=(float)works[i].T_Need/works[i].T_Serve;
        }
    }
    //输出数据
    public void DataOut(){
        System.out.print("作 业 名：");
        for (int i=0;i< works.length;i++){
            if (i==works.length-1){
                System.out.println(works[i].WorkName);break;
            }
            System.out.print(works[i].WorkName+" ");
        }
        System.out.print("到达时间：");
        for (int i=0;i< works.length;i++){
            if (i==works.length-1){
                System.out.println(works[i].T_Come);break;
            }
            System.out.print(works[i].T_Come+" ");
        }
        System.out.print("服务时间：");
        for (int i=0;i< works.length;i++){
            int x=(int)(works[i].T_Serve);
            if (i==works.length-1){
                System.out.println(x);break;
            }
            System.out.print(x+" ");
        }
        System.out.print("完成时间：");
        for (int i=0;i< works.length;i++){
            if (i==works.length-1){
                System.out.println(works[i].T_Compelet);break;
            }
            System.out.print(works[i].T_Compelet+" ");
        }
        System.out.print("周转时间：");
        for (int i=0;i< works.length;i++){
            if (i==works.length-1){
                System.out.println(works[i].T_Need);break;
            }
            System.out.print(works[i].T_Need+" ");
        }
        System.out.print("带权周转时间：");
        for (int i=0;i< works.length;i++){
            if (i==works.length-1){
                System.out.println(String.format("%.2f",works[i].T_Weight));break;
            }
            System.out.print(String.format("%.2f",works[i].T_Weight)+" ");
        }
    }
}

